#include "DBOption/Utility/ConnectorHelp.h"
#include "base/core/com_define.h"
#include "base/core/os/time.h"
#include "DBOption/Redis/RedisCommand.h"
#include "DBOption/Mysql/MysqlConnector.h"
#include "DBOption/Redis/RedisConnector.h"

bool ConnectToMySQL(
					const char *container, 
					MysqlConnector* connector,
					const MYSQL_SERVER_PARAM& param,
					bool* threadExitFlag
					)
{
    bool result = false;

    MMO_ASSERT(container);
    MMO_ASSERT(connector);
    MMO_ASSERT(threadExitFlag);

    LogInfoFmtPrint(
					"[%s] try connect to mysql server %s:%d(db=%s)...",
					container, param.ip.c_str(), param.port, param.database.c_str()
					);

    while (!(*threadExitFlag)) 
	{
        if (connector->Connect(
								param.ip.c_str(),
								param.port,
								param.user.c_str(),
								param.password.c_str(),
								param.database.c_str(),
								param.characterSet.c_str()
								)) 
		{
            break;
        }
        Time::Sleep(500);
    }
    MMO_PROCESS_SUCCESS(*threadExitFlag);

    LogInfoFmtPrint(
					"[%s] connected to mysql server %s:%d(db=%s) success.",
					container, param.ip.c_str(), param.port, param.database.c_str()
					);
Exit1:
    result = true;
//Exit0:
    return result;
}

bool ConnectToRedis(
					const char *container,
					RedisConnector *connector,
					const REDIS_SERVER_PARAM &param,
					bool *threadExitFlag
					)
{
    bool result = false;

    MMO_ASSERT(container);
    MMO_ASSERT(connector);
    MMO_ASSERT(threadExitFlag);

    LogInfoFmtPrint(
					"[%s] try connect redis server (%s:%d)...",
					container, param.ip.c_str(), param.port
					);

    while (!(*threadExitFlag)) 
	{
        if (connector->Connect(
								param.ip.c_str(),
								param.port,
								param.password.c_str()
								)) 
		{
            break;
        }
        Time::Sleep(500);
    }
    MMO_PROCESS_SUCCESS(*threadExitFlag);
    LogInfoFmtPrint(
					"[%s] connected to redis server (%s:%d) success.",
					container, param.ip.c_str(), param.port
					);
Exit1:
    result = true;
// Exit0:
    return result;
}
